package cn.didi.feidi.androidorder.assembly;

import android.app.Dialog;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import cn.didi.feidi.androidorder.R;
import cn.didi.feidi.androidorder.databinding.DialogFilterBinding;

/**
 * Created by jinxiong on 2017/8/24.
 */

public class FeiDiFilterFragmentDialog extends DialogFragment {

    private final String price_0_20 = "0元-20元";
    private final String price_20_40 = "20元-40元";
    private final String price_40_60 = "40元-60元";
    DialogFilterBinding binding;
    private CallBack mCallBack;

    private float startPrice;
    private float endPrice;
    private boolean clickFlag;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getArguments();

        if (bundle != null) {
            startPrice = bundle.getFloat("startPrice");
            endPrice = bundle.getFloat("endPrice");
        }

    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.dialog_filter, container, false);

        addListener();
        initView();


        return binding.getRoot();
    }

    private void initView() {

        if (startPrice == 0 && endPrice == 20) {
            binding.filterDialog20Tv.setSelected(true);
        } else if (startPrice == 21 && endPrice == 40) {
            binding.filterDialog40Tv.setSelected(true);
        } else if (startPrice == 41 && endPrice == 60) {
            binding.filterDialog60Tv.setSelected(true);
        }

        if (startPrice == 0 && endPrice == Float.MAX_VALUE) {

        } else {
            binding.filterDialogStartPriceEt.setText(startPrice + "");
            binding.filterDialogEndPriceEt.setText(endPrice + "");
        }


    }

    private void addListener() {

        binding.filterDialogSubmitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dismiss();

                if (mCallBack != null) {

                    if (startPrice > endPrice) {
                        Toast.makeText(getActivity(), "价格区间有误", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    mCallBack.callBack(startPrice, endPrice);

                }
            }
        });

        binding.filterDialogDefaultTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.filterDialog20Tv.setSelected(false);
                binding.filterDialog40Tv.setSelected(false);
                binding.filterDialog60Tv.setSelected(false);

                startPrice = 0;
                endPrice = Float.MAX_VALUE;

                binding.filterDialogStartPriceEt.setText("");
                binding.filterDialogEndPriceEt.setText("");


            }
        });


        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                clickFlag = true;
                TextView textView = (TextView) v;
                binding.filterDialog20Tv.setSelected(false);
                binding.filterDialog40Tv.setSelected(false);
                binding.filterDialog60Tv.setSelected(false);
                textView.setSelected(true);


                if (textView == binding.filterDialog20Tv) {
                    startPrice = 0;
                    endPrice = 20;
                } else if (textView == binding.filterDialog40Tv) {
                    startPrice = 21;
                    endPrice = 40;
                } else {
                    startPrice = 41;
                    endPrice = 60;
                }

                binding.filterDialogStartPriceEt.setText(startPrice + "");
                binding.filterDialogEndPriceEt.setText(endPrice + "");

            }
        };
        binding.filterDialog20Tv.setOnClickListener(listener);
        binding.filterDialog40Tv.setOnClickListener(listener);
        binding.filterDialog60Tv.setOnClickListener(listener);


        binding.filterDialogStartPriceEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {

                if (clickFlag) {
                    return;
                }

                binding.filterDialog20Tv.setSelected(false);
                binding.filterDialog40Tv.setSelected(false);
                binding.filterDialog60Tv.setSelected(false);


                if (s.length() <= 0) {
                    return;
                }

                try {

                    float tem = Float.parseFloat(s.toString());

                    if (tem < 0) {
                        Toast.makeText(getActivity(), "输入正数", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    startPrice = tem;

                } catch (NumberFormatException e) {
                    Toast.makeText(getActivity(), "输入数字", Toast.LENGTH_SHORT).show();
                }


            }
        });

        binding.filterDialogEndPriceEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (clickFlag) {
                    clickFlag = false;
                    return;
                }
                binding.filterDialog20Tv.setSelected(false);
                binding.filterDialog40Tv.setSelected(false);
                binding.filterDialog60Tv.setSelected(false);

                if (s.length() <= 0) {
                    return;
                }


                try {
                    float tem = Float.parseFloat(s.toString());

                    if (tem < 0) {
                        Toast.makeText(getActivity(), "输入正数", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    endPrice = tem;

                } catch (NumberFormatException e) {
                    Toast.makeText(getActivity(), "输入数字", Toast.LENGTH_SHORT).show();
                }


            }
        });

    }

    public void setCallBack(CallBack callBack) {
        mCallBack = callBack;
    }


    public interface CallBack {
        void callBack(float startPrice, float endPrice);
    }
}
